﻿@{
    ViewBag.Title = "MenuActionList";
    Layout = "~/Areas/Admin/Views/Shared/_LayoutForm.cshtml";
}

<div class="layui-col-md12 top10">
    <form class="layui-form" id="form" onsubmit="return false;">
        <input type="hidden" id="hidfMenuId" value="@ViewBag.Id" />
        <div class="layui-form-item">
            <label class="layui-form-label">菜单名称：</label>
            <div class="layui-input-inline">
                <input class="layui-input" type="text" value="@ViewBag.MenuName" disabled>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">菜单权限：</label>
            <div class="layui-input-block">
                @if (ViewBag.ParentId == 0)
                {
                    <input class="layui-input" type="text" value="含有子菜单的父菜单不能设置权限" disabled>
                }
                else
                {
                    @Html.ActionCheckBox(ViewData["MenuActionList"], ViewData["AvailableMenuActionList"])
                }
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" id="submitForm">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['common', 'form'], function () {
        var common = layui.common,
            form = layui.form;
        var $ = layui.$;
        //关闭页面
        function CloseWin() {
            parent.location.reload(); // 父页面刷新
            //var _btnSearch = window.parent.document.getElementById("btnSearch");  这里注释是因为菜单管理没有搜索按钮
            //if (_btnSearch) {
            //    _btnSearch.click();
            ////}
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        }
        //保存设置
        $("#submitForm").on('click', function () {
            var _menuId = $("#hidfMenuId").val();
            var cbxs = $(".layui-form").find('input[type="checkbox"]');
            var _list = [];
            cbxs.each(function (index, item) {
                if (item.checked == true) {
                    var _row = { MenuId: 0, ActionId: 0 };
                    _row.MenuId = _menuId;
                    _row.ActionId = item.value;
                    _list.push(_row);
                }
            });
            console.log(_list);
            $.ajax({
                url: "/Permissions/MenuAction/InsertBatch",
                type: "POST",
                data: { "list": _list, menuId: _menuId },
                dataType: "json",
                success: function (data) {
                    if (data.state == "success") {
                        common.msgSuccess("保存成功");
                    } else {
                        common.msgError("保存失败");
                    }
                    setTimeout(CloseWin, 1500);
                }
            });
        });
    });
</script>

